package com.focusai.efairy.model.manager;

import android.content.SharedPreferences;

import com.focusai.efairy.model.response.LoginResponse;
import com.focusai.efairy.model.user.User;
import com.focusai.efairy.utils.PreferenceKeys;
import com.focusai.efairy.utils.PreferencesUtil;

/**
 * Created by luo_guangwen on 2017/8/12.
 */

public class CacheManager {
    /**
     * 保存登录后的信息
     */
    public static void saveLoginInfo(LoginResponse loginResponse,String phone) {
        if (loginResponse == null)
            return;
        PreferencesUtil.getInstance().putString(PreferencesUtil.PrefType.temp, PreferenceKeys.ACCESS_TOKEN, loginResponse.access_token);
        PreferencesUtil.getInstance().putString(PreferencesUtil.PrefType.temp, PreferenceKeys.RONGCLOUD_TOKEN, loginResponse.rongcloud_token);
        User user = loginResponse.user_info;
        if (user != null) {
            PreferencesUtil.getInstance().putLong(PreferencesUtil.PrefType.system, PreferenceKeys.CURRENT_LOGIN_USER_ID, user.getEfairyuser_id());
            PreferencesUtil.getInstance().putString(PreferencesUtil.PrefType.system, PreferenceKeys.LAST_LOGIN_USER_PHONE, phone);
            SharedPreferences sharedPreferences = PreferencesUtil.getServiceSharedPreferences(user.getEfairyuser_id());
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(PreferenceKeys.EFAIRYUSER_ID, "" + user.getEfairyuser_id());
            editor.putString(PreferenceKeys.EFAIRYUSER_CITY, user.getEfairyuser_city());
            editor.putString(PreferenceKeys.EFAIRYUSER_COUTRY, user.getEfairyuser_coutry());
            editor.putString(PreferenceKeys.EFAIRYUSER_HEADIMG_URL, user.getEfairyuser_headimg_url());
            editor.putInt(PreferenceKeys.EFAIRYUSER_LAST_LOGIN_METHOD, user.getEfairyuser_last_login_method());
            editor.putString(PreferenceKeys.EFAIRYUSER_LAST_LOGIN_TIME, user.getEfairyuser_last_login_time());
            editor.putString(PreferenceKeys.EFAIRYUSER_NICKNAME, user.getEfairyuser_nickname());
            editor.putString(PreferenceKeys.EFAIRYUSER_PROVINCE, user.getEfairyuser_province());
            editor.putString(PreferenceKeys.EFAIRYUSER_PROVINCE, user.getEfairyuser_province());
            editor.putInt(PreferenceKeys.EFAIRYUSER_SEX, user.getEfairyuser_sex());
            editor.putInt(PreferenceKeys.IS_NORMAL_USER, loginResponse.is_normal_user);
            editor.putInt(PreferenceKeys.ROLE_LEVEL, user.getEfairyrole_level());
            editor.commit();
        }
    }

    public static void loginOut() {
        PreferencesUtil.getInstance().putLong(PreferencesUtil.PrefType.system, PreferenceKeys.CURRENT_LOGIN_USER_ID, 0);
        PreferencesUtil.getInstance().getSharePref(PreferencesUtil.PrefType.temp).edit().clear().commit();
        PreferencesUtil.getCurrentUserSharedPreferences().edit().clear().commit();
    }

    public static String getAccessToken() {
        return PreferencesUtil.getInstance().getString(PreferencesUtil.PrefType.temp, PreferenceKeys.ACCESS_TOKEN, "");
    }

    public static String getRoogCloudAccessToken() {
        return PreferencesUtil.getInstance().getString(PreferencesUtil.PrefType.temp, PreferenceKeys.RONGCLOUD_TOKEN, "");
    }

    public static String getUserId() {
        return PreferencesUtil.getCurrentUserSharedPreferences().getString(PreferenceKeys.EFAIRYUSER_ID, "");
    }

    public static String getUserName() {
        return PreferencesUtil.getCurrentUserSharedPreferences().getString(PreferenceKeys.EFAIRYUSER_NICKNAME, "");
    }

    public static void setUserName(String name) {
        PreferencesUtil.getCurrentUserSharedPreferences().edit().putString(PreferenceKeys.EFAIRYUSER_NICKNAME, name).commit();
    }

    public static String getUserAvater() {
        return PreferencesUtil.getCurrentUserSharedPreferences().getString(PreferenceKeys.EFAIRYUSER_HEADIMG_URL, "");
    }

    public static void setUserAvater(String avaterUrl) {
        PreferencesUtil.getCurrentUserSharedPreferences().edit().putString(PreferenceKeys.EFAIRYUSER_HEADIMG_URL, avaterUrl).commit();
    }

    /**
     * 0-超级管理员 1-一级经销商 2-二级经销商 3-项目管理员 4-普通用户
     * */
    public static int getUserRoleLevel() {
        return PreferencesUtil.getCurrentUserSharedPreferences().getInt(PreferenceKeys.ROLE_LEVEL, 0);
    }

    public static boolean isNormalUser() {
        return PreferencesUtil.getCurrentUserSharedPreferences().getInt(PreferenceKeys.IS_NORMAL_USER, 0) == 1;
    }

    public static void setIsgoreVersion(int version) {
        PreferencesUtil.getInstance().putInt(PreferencesUtil.PrefType.system, PreferenceKeys.INGORE_UPDATE, version);
    }

    public static int getIsgoreVersion() {
        return PreferencesUtil.getInstance().getInt(PreferencesUtil.PrefType.system, PreferenceKeys.INGORE_UPDATE, 0);
    }

    public static void setUserMsgTip(boolean open) {
        PreferencesUtil.getCurrentUserSharedPreferences().edit().putBoolean(PreferenceKeys.SERIVCE_NEW_MSG_TIP, open).commit();
    }


    public static boolean getUserMsgTip() {
        return PreferencesUtil.getCurrentUserSharedPreferences().getBoolean(PreferenceKeys.SERIVCE_NEW_MSG_TIP, true);
    }

    public static void setLastVersionCode(int versionCode) {
        PreferencesUtil.getCurrentUserSharedPreferences().edit().putInt(PreferenceKeys.LAST_VERSION_CODE, versionCode).commit();
    }

    public static int getLastVersionCode() {
        return PreferencesUtil.getCurrentUserSharedPreferences().getInt(PreferenceKeys.LAST_VERSION_CODE, 0);
    }
}
